package forum.service.rest;

import com.foreveross.springboot.dubbo.utils.Payload;
import forum.domain.Role;
import forum.domain.RoleResources;
import forum.domain.Token;
import forum.domain.UserRole;
import forum.service.rest.api.RoleRestService;
import forum.service.rpc.api.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/1.
 */
@Service("roleRestService")
@RestController
@RequestMapping(value = "/apis/role")
public class RoleRestServiceImpl implements RoleRestService {

    @Autowired(required=true)
    private RoleService roleService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleResourcesService roleResourcesService;
    Map<String,String> error = new HashMap<String, String>();
    Map<String,Object> success = new HashMap<String, Object>();

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 带条件的分页获取user对象 一些参数非必须
     * @param page
     * @param size
     * @param sort
     * @param operation
     * @param key
     * @param value
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
//    @RequiresPermissions("create")
    public Payload getRoleList(@QueryParam("page") @DefaultValue("0") int page,
                               @QueryParam("size") @DefaultValue("50") int size,
                               @QueryParam("sort") @DefaultValue("sort=roleid,desc") String sort,
                               @QueryParam("operation") @DefaultValue("operation=eq") String operation,
                               @QueryParam("key") @DefaultValue("key=roleid") String key,
                               @QueryParam("value") @DefaultValue("1") String value,
                               @QueryParam("token") @DefaultValue("null") String token) {

        List<Token> tokenlist =new ArrayList<Token>();
        if (token==null){
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        }else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist!=null&&tokenlist.size()>0) {

            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>"+ roleList.get(0).getRole().getRolename().toString().trim() +"<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:role:getList") || list.getResources().getResourcesname().equals("role")) {
                            logger.info("你刚好有权限标志<------>apis:user:get<------>||<------>role<------>");
                            //这里开始搞事情
                            success.put("content",roleService.getRoleList(page,size,sort,operation,key,value));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(roleService.getRoleList(page,size,sort,operation,key,value));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }

        }else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/role/getList访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/role/getList访问权限");
    }

    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    public Payload getRoleById (@PathVariable Integer id){
        return new Payload(roleService.getRoleById(id));
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Payload createRole(@QueryParam("token") @DefaultValue("null") String token,@RequestBody Role role){
        List<Token> tokenlist =new ArrayList<Token>();
        if (token==null){
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        }else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist!=null&&tokenlist.size()>0) {

            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>"+ roleList.get(0).getRole().getRolename().toString().trim() +"<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:role:create") || list.getResources().getResourcesname().equals("role")) {
                            logger.info("你刚好有权限标志<------>apis:user:create<------>||<------>role<------>");
                            //这里开始搞事情
                            success.put("content",roleService.createRole(role));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(roleService.createRole(role));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }


        }else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/role/create访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/role/create访问权限");
    }

    @RequestMapping(value = "/updateById", method = RequestMethod.PUT)
    public Payload updateRoleById(@QueryParam("token") @DefaultValue("null") String token,@RequestBody Role role){
        List<Token> tokenlist =new ArrayList<Token>();
        if (token==null){
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        }else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist!=null&&tokenlist.size()>0) {

            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>"+ roleList.get(0).getRole().getRolename().toString().trim() +"<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:role:updateById") || list.getResources().getResourcesname().equals("role")) {
                            logger.info("你刚好有权限标志<------>apis:user:updateById<------>||<------>role<------>");
                            //这里开始搞事情
                            success.put("content",roleService.updateRoleById(role));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(roleService.updateRoleById(role));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }


        }else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/role/updateById访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/role/updateById访问权限");
    }

    @RequestMapping(value = "/deleteByIds", method = RequestMethod.DELETE)
    public Payload deleteRoleByIds(@QueryParam("token") @DefaultValue("null") String token,@RequestBody Map<String, String[]> map) {


        List<Token> tokenlist =new ArrayList<Token>();
        if (token==null){
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        }else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist!=null&&tokenlist.size()>0) {

            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>"+ roleList.get(0).getRole().getRolename().toString().trim() +"<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:role:deleteByIds") || list.getResources().getResourcesname().equals("role")) {
                            logger.info("你刚好有权限标志<------>apis:user:deleteByIds<------>||<------>role<------>");
                            //这里开始搞事情
                            String[] stringIds = map.get("rows");
                            if (stringIds != null && stringIds.length > 0) {
                                Integer[] roleIds = new Integer[stringIds.length];
                                //转换为int数组
                                for (int i = 0; i < stringIds.length; i++) {
                                    roleIds[i] = Integer.parseInt(stringIds[i]);
                                }
                                success.put("content",roleService.deleteRoleByIds(roleIds));
                                success.put("success",1);
                                return new Payload(success);
//                                return new Payload(roleService.deleteRoleByIds(roleIds));
                            }
                            error.put("msg","false");
                            error.put("fail","0");
                            return new Payload(error);
//                            return new Payload(false);
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }

        }else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/role/deleteByIds访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/role/deleteByIds访问权限");
    }

}
